package service;

import util.CheckParameters;
import dao.RoommateDAO;
import domain.Balance;
import domain.Roommate;
import exception.InvalidBusinessStateException;

public class RoommateService extends GenericService<Roommate> {

	private static RoommateService instance;

	public static RoommateService getInstance() {
		if (instance == null)
			instance = new RoommateService();
		return instance;
	}

	public RoommateService() {
		dao = RoommateDAO.getInstance();
	}

	@Override
	protected void businessRules(Roommate roommate)
			throws InvalidBusinessStateException {
		CheckParameters.checkStringEmpty(roommate.getName(), "Roommate's name");
	}

	@Override
	protected void handleDependencies(Roommate entity)
			throws InvalidBusinessStateException {}

	@Override
	protected void cascadeDeletion(Integer id)
			throws InvalidBusinessStateException {
		Balance balance = BalanceService.getInstance().findByRoommate(id);
		if (balance.getBalance() != 0.0) {
			throw new InvalidBusinessStateException(
					"Roommate cannot be deleted if his balance isn't closed dumbass!");
		}
		BalanceService.getInstance().delete(balance.getId());
	}

}
